﻿package cgs.Cache
{
    import flash.net.*;

    public class Cache extends Object
    {
        static var m_cache:SharedObject;

        public function Cache()
        {
            return;
        }// end function

        static function get cacheExists() : Boolean
        {
            if (m_cache)
            {
                return true;
            }
            try
            {
                m_cache = SharedObject.getLocal("userData");
                return true;
            }
            catch (err:Error)
            {
            }
            return false;
        }// end function

        public static function get size() : uint
        {
            return cacheExists ? (m_cache.size) : (0);
        }// end function

        public static function clearCache() : void
        {
            if (cacheExists)
            {
                m_cache.clear();
            }
            return;
        }// end function

        public static function deleteSave(param1:String) : void
        {
            var property:* = param1;
            if (cacheExists && saveExists(property))
            {
                delete m_cache.data[property];
                try
                {
                    m_cache.flush();
                }
                catch (err:Error)
                {
                }
            }
            return;
        }// end function

        public static function getSave(param1:String)
        {
            return cacheExists ? (m_cache.data[param1]) : (null);
        }// end function

        public static function initSave(param1:String, param2) : void
        {
            var property:* = param1;
            var defaultVal:* = param2;
            if (!cacheExists)
            {
                return;
            }
            if (!m_cache.data.hasOwnProperty(property))
            {
                m_cache.data[property] = defaultVal;
            }
            try
            {
                m_cache.flush();
            }
            catch (err:Error)
            {
            }
            return;
        }// end function

        public static function saveExists(param1:String) : Boolean
        {
            return cacheExists ? (m_cache.data.hasOwnProperty(param1)) : (false);
        }// end function

        public static function setSave(param1:String, param2) : Boolean
        {
            var property:* = param1;
            var val:* = param2;
            if (!cacheExists)
            {
                return false;
            }
            m_cache.data[property] = val;
            try
            {
                m_cache.flush();
            }
            catch (err:Error)
            {
            }
            return true;
        }// end function

    }
}
